<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    handleOfficeLineCharts:{
      type: Function,
      default: null
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      var that = this;
      this.chart.on('click',function(params){
        var answer = params.seriesName;
        var number = params.name;
        var countValue = params.value;
        var index = params.dataIndex;
        var data = that.chartData.zdata[index];
        var params = that.$parent.addDateRange(that.$parent.queryParams,that.$parent.dateRange);
        params.deptId = data;
 　　　  that.handleOfficeLineCharts(params);//调用接口
      })
      
    },
    setOptions( {xdata,ydata,zdata,adata,bdata,pdata,tdata}  = {}) {
      this.chart.setOption({
        color: ['#3398DB'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {            // 坐标轴指示器，坐标轴触发有效
              type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
          },
          // formatter: function(params){
          //   console.log(params);
          //     let totalValue = params[0].value + params[1].value;
          //     var index = params[0].dataIndex;
          //     return params[0].axisValueLabel+"</br>"+"总和："+totalValue+"</br>"+
          //     params[0].seriesName+":"+params[0].value+"</br>"+params[1].seriesName+":"+params[1].value+"</br>";
          //   }
        },
        title: {
            text: bdata,
            textStyle: {
              fontSize: 15,
              color: "#d5e2f6"
            },
            left: '28',
            top: "12"
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [{
            type: "category",
            axisLine: {
                lineStyle: {
                    color: '#4693EC'
                },
                
            },
            splitLine: {
                "show": false
            },
            axisTick: {
                "show": false
            },
            splitArea: {
                "show": false
            },
            axisLabel: {
                interval: 0,
                rotate:"45",
                color: "#798596",
                fontSize:12,
                formatter: function (params) {
                  var newParamsName = "";
                  var paramsNameNumber = params.length;
                  if(paramsNameNumber>=8){
                      newParamsName = params.substring(0,3)+"..."+params.substring(paramsNameNumber-3,paramsNameNumber);
                  }else{
                    newParamsName = params
                  }
                  return newParamsName
              }
            },
            data: xdata,
        }],
        yAxis: [{
            type: "value",
            splitLine: {
                lineStyle: {
                  type: 'dotted' // y轴分割线类型
                },
                "show": true
            },
            show: true,
            axisLine: {
                show: false,
                lineStyle: {
                    type: 'dashed',
                    color: '#4693EC'
                }
            },
            axisTick: {
                "show": true
            },
            axisLabel: {
                interval: 0,
                rotate:"45",
                color: "#00FFE3",
                fontSize:12
            },
            splitArea: {
                "show": false
            },

        }],
        series: [
         
        {
            type: "bar",
            stack: '工作',
            name: "日常工作",
            label: {
                    show: true,
                    color: '#ffe7ff',
                    position: 'inside' 
                },
            itemStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: "#ee9c4f"
                        },
                        {
                            offset: 1,
                            color: "#ee9c4f"
                        }
                    ])
                }
            },
            data:ydata
        },
         {
            type: "bar",
            name: "项目工作",
            stack: '工作',
            label: {
                    show: true,
                    color: '#a0ffff',
                    position: 'inside'
                },
            itemStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: "#a0ffff"
                        },
                        {
                            offset: 1,
                            color: "#a0ffff"
                        }
                    ])
                }
            },
            data:pdata
        },
        {
            type: "bar",
            stack: '工作',
            name: "总量",
            label: {
                   normal: {
                        offset:['50', '80'],
                        show: true,
                        position: 'insideBottom',
                        formatter:'{c}',
                        textStyle:{ color:'#fff' }
                    }

                },
            itemStyle: {
              normal:{
                  color:'rgba(128, 128, 128, 0)'
              }
            },
            data:tdata
        }
        
      ]
     })
    }
  }
}
</script>
